<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="../includes/tag.jsp" %>
<%@taglib prefix="bstag" uri="/core_taglib" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Wcms内容管理系统</title>
    <script type="text/javascript" src="<%=basePath%>resources/lib/jquery/jquery.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.core.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/i18n/grid.locale-cn.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/jquery.jqGrid.min.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/public.js"></script>
	
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/styles/themes/buss-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/jquery-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/ui.jqgrid.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/public.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/icon.css">

	<script type="text/javascript">
		$(document).ready(function(){						
			setGridData();
			loadTree();
			resize();
			$(window).resize(resize);
			$("#add-btn").button({iconCls:'icon-add',handler:function(){
				var node = $("#sysmenuTree").treeview("getSelectedNode");
				if(!node){
					$.alert('提示','请选择要增加菜单的父节点!',300,140,'info');
					return;
				}
				var url = '<%=basePath%>admin/sysmenu/addpage.do';
				var addUrl = '<%=basePath%>admin/sysmenu/add.do';
				var dlg = syscore.editDialog('新增系统菜单信息',url,'id=' + node.id,500,350,addUrl,function(rsData){
				
					if(rsData.message.code != '0'){
						 $.alert('提示','保存数据失败!原因:' + rsData.message.mess,300,140,'error');
						 return false;
					 }else{
						 var node = $("#sysmenuTree").treeview("getSelectedNode");
	       				 $("#sysmenuTree").treeview("addNodes",node,rsData.sysmenu);
	       			 	 $("#tabledata").jqGrid().trigger('reloadGrid');				        			 	
	       			 	 dlg.dialog("destroy");
					 }	
				});
			}});		
			$("#edit-btn").button({iconCls:'icon-edit',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows == 0){
					$.alert('提示','你没有选择要修改的数据!',300,140,'info');
					return;
				}
				if(selRows.length > 1){
					$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
					return;
				}
				editSysmenu(selRows[0]);			
			}});
			$("#del-btn").button({iconCls:'icon-remove',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows == 0){
					$.alert('提示','你没有选择要删除的数据!',300,140,'info');
					return;
				}
				$.confirm('提示','确认要删除选中数据吗?',300,140,function(){
					var str = "";
					for(var i = 0;i < selRows.length;i++){
						if(i < selRows.length - 1){
							str += "ids=" + selRows[i] + "&";
						}else{
							str += "ids=" + selRows[i];
						}
					}
					$.post("<%=basePath%>admin/sysmenu/delete.do",str,function(rsData){
						if(rsData.message.code != '0'){
							$.alert('错误','删除系统菜单信息失败!原因:' + rsData.message.mess,300,140,'error');
							return;
						}
						for(var i = 0;i < selRows.length;i++){
							 var node = $("#sysmenuTree").treeview("getNodeByParam","id",selRows[i]);        				
	        				 $("#sysmenuTree").treeview("removeNode",node);			        				 
						}
						$("#tabledata").jqGrid({page:1}).trigger('reloadGrid');
					},"json");
				});				
			}});
			
		});
		
		function editSysmenu(menuId){
			var url = '<%=basePath%>admin/sysmenu/editpage.do';
			var editUrl = '<%=basePath%>admin/sysmenu/edit.do';
			var dlg = syscore.editDialog('修改系统菜单信息',url,'id='+menuId,500,350,editUrl,function(rsData){
				if(rsData.message.code != '0'){
					 $.alert('提示','保存数据失败!原因:' + rsData.message.mess,300,140,'error');
					 return false;
				 }else{
					 var tId = dlg.find("input[name='id']").val();
    				 var node = $("#sysmenuTree").treeview("getNodeByParam","id",tId);
    				 node.name = dlg.find("input[name='name']").val();
    				 node.code = dlg.find("input[name='code']").val();
    				 $("#sysmenuTree").treeview("updateNode",node);			        				 
    				 
    			 	 $("#tabledata").jqGrid().trigger('reloadGrid');			        			 	
       			 	 dlg.dialog("destroy");
				 }	
			});	
		}
		
		function postEditMenu(menuId){
			editSysmenu(menuId);
		}
		
		function setGridData(){
			var url = "<%=basePath%>admin/sysmenu/list/0.do";
			var colNames = ['id','菜单名称','菜单编码', '是否停用','类型','排序序号'];
			var colModel = [
							{name:'id',index:'id', width:60, hidden:true},
						    {name:'name',index:'name', width:100,align:'left',formatter:function(cellValue,opts,rowObj){
						    	
						    	return "<a  style='text-decoration: underline;' href='javascript:postEditMenu("+rowObj.id+");'>"+cellValue+"</a>";
						    }},
							{name:'code',index:'code', width:60,align:'left'}, 
							{name:'disabled',index:'disabled', width:60,align:'center',formatter:function(cellValue){
								if(cellValue == true){
									return "停用";
								}else{
									return "启用";
								}
							}}, 
							{name:'menuType.value', width:80,align:'center'},
							{name:'seqNum',index:'seqNum', width:30,align:'center'}
						];
			syscore.setGridData("#tabledata","",url,colNames,colModel,true);
		}	
		
		function loadTree(){
			var root = [{
				id:"0",
				code:'menu_root',
				name:'系统菜单信息',			
				isParent:true,
				hasNode:true,
				open:true
			}];
			$("#sysmenuTree").treeview({
				treeNodeKey:'id',
				treeNodeParentKey:'parentId',
				hasChildKey:'hasNode',
				nameCol:'name',
				nodesCol:'nodes',
				expandSpeed:"",
				asyncParam:['parentId'],
				async:true,
				root:{isRoot:true,nodes:root},
				asyncUrl:function(treeNode){
					if(treeNode){
						return "<%=basePath%>admin/sysmenu/listByTree/"+treeNode.id+".do";
					}else{
						return "<%=basePath%>admin/sysmenu/listByTree/0.do";
					}
				},
				callback:{
					beforeAsyncSuccess:function(msg){
						if(msg.message.code == '-2'){
							$.alert('提示','你还没有登录或Session已经过期，请重新登录系统!',300,170,'info');
							return false;
						}
						if(msg.message.code == "-1"){
							$.alert("提示","获取信息失败，原因：" + msg.message.mess,300,170,'error');
							return false;
						}
						return true;
					},
					click:function(event,treeId,treeNode){
						var url = $("#tabledata").jqGrid("getGridParam","url");
						//alert(url);
						$("#tabledata").jqGrid("setGridParam",{"url":"<%=basePath%>admin/sysmenu/list/" + treeNode.id + ".do"}).trigger('reloadGrid');
						//alert(treeNode.id);
					}
				}
			});
		}
		
		

		
		function resize(){
			var h = $(window).height() - $(".nav_curloc").outerHeight() - $(".nav_search").outerHeight() - $(".nav_btn_bar").outerHeight() - 10;
			$(".context").height(h);
			$("#tabledata").jqGrid("setGridHeight",h - 25);
			$("#tabledata").jqGrid("setGridWidth",$(window).width() - $("#sysmenuTree").parent().width() - 12);
		}
		
	</script>
  </head>
  
  <body>
  	<div style="border-right:1px solid #68C4E9;position:absolute;width:180px;height:100%;left:0;overflow:auto;">
  		<ul id="sysmenuTree" class="tree"></ul>
  	</div>
  	<div style="height:100%;margin:0 0 0 181px;position:relative;">
  		<div class="nav_curloc">
	    	<label>您的当前位置：<bstag:navbar menuId="${menuid}"/></label>
	    </div>
	  <!--   <div class="nav_search" style="height:28px;">
	    	<table height="100%" cellpadding="0" cellspacing="0">
	    		<tr>
	    			<td valign="middle"">菜单名称：</td>
	    			<td valign="middle"">
	    				<input type="text" size="25" maxlength="100" class="text" name="menu_name"/>
	    			</td>
	    			<td style="padding-left:5px;">
	    				<a id="search-btn" href="javascript:void(0);">查 询</a>
	    			</td>
	    		</tr>
	    	</table>
	    </div> -->
	    <div class="nav_btn_bar">
	    	<table height="100%" cellpadding="0" cellspacing="0">
	    		<tr>
	    			<td valign="middle">
	    				<a id="add-btn" href="javascript:void(0);">新 增</a>
	    			</td>
	    			<td valign="middle"" style="padding-left:5px;">
	    				<a id="edit-btn" href="javascript:void(0);">修 改</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="del-btn" href="javascript:void(0);">删 除</a>
	    			</td>
	    		</tr>
	    	</table>
	    </div>
	    <div class="context">
	    	<table id="tabledata"></table>
	    	<div id="tablepage"></div>
	    </div>
  	</div> 
    
  </body>
</html>
